export default {
	//验证支付结果 - 成功
	appPaySuccess(succ) {
		succ();
	},
	//验证支付结果 - 失败
	appPayFail(fail) {
		fail();
	},
	//APP支付
	appPay(data = {}, succ, fail) {
		let that = this;
		//data：参数
		//succ：成功回调
		//fail：失败回调
		let type = data.type; //支付方式 2：微信 1：支付宝app 3：支付宝H5
		let pay_data = data.data; //支付参数  参数或者地址
		//调用支付
		switch (type) {
			case 2:
				//微信
				uni.requestPayment({
					provider: 'wxpay',
					orderInfo: pay_data, //微信、支付宝订单数据
					success: function(res) {
						that.appPaySuccess(succ);
					},
					fail: function(err) {
						that.appPayFail(fail);
					}
				});
				break;
			case 1:
				//支付宝
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: pay_data,
					success: function(res) {
						console.log(res)
						that.appPaySuccess(succ);
					},
					fail: function(err) {
						console.log(err)
						that.appPayFail(fail);
					}
				});
				break
			case 3:
				//支付宝 - H5
				if (that.getStorageSyncVal('sysTypes') == 'ios') {
					plus.runtime.launchApplication({
						action: orderInfo
					}, function(res) {
						uni.showToast({
							icon: "none",
							title: "请安装支付宝之后再进行支付！",
						});
					});
				} else {
					plus.runtime.openURL(orderInfo, function(res) {
						uni.showToast({
							icon: "none",
							title: "请安装支付宝之后再进行支付！",
						});
					});
				}
				break
		}
	},
	//图片预览
	previewImg(current, imgs) {
		uni.previewImage({
			current: current,
			urls: imgs,
		});
	},
	//复制 小程序/APP
	copyStr(str) {
		uni.setClipboardData({
			data: str,
			success() {
				this.msg('复制成功');
			}
		});
	},
	isPath(url) {
		let list = ['pages2/wentiku/wentiku', 'page3/shushouyouhuizhengce/shushouyouhuizhengce', 'pages2/kjfgk/kjfgk',
			'school/main/main'
		]
		for (let item of list) {
			if (url.indexOf(item) != -1) return true
		}
		return false
	},
	//退出
	outFun() {
		this.removeStorageSyncVal('token');
		this.removeStorageSyncVal('userInfo');
		uni.switchTab({
			url: '/pages/index/index'
		})
	},
	//没有登录或者登录失效 清除缓存 跳转到首页
	noTokenGo() {
		console.log("noTokenGo");
		// this.msg('请登陆后操作');

		// uni.clearStorage()
		this.removeStorageSyncVal('token');
		this.removeStorageSyncVal('userInfo');
		uni.removeTabBarBadge({
			index: 3
		})
		uni.hideLoading()


		const platform = uni.getSystemInfoSync().platform
		console.log(platform)
		if (platform != 'ios') {
			uni.reLaunch({
				url: '/pages1/login/login'
			});
		} else {
			uni.navigateTo({
				url: '/pages1/login/login'
			});
			return;

		}
	},
	// 显示无权查看弹窗
	showModel() {
		uni.showModal({
			title: '温馨提示',
			content: '普通用户每天可浏览20条数据\n成为小竹会员后免费查看',
		});
	},
	//初始化数据
	initApp(that) {
		let systemInfo = uni.getSystemInfoSync();
		let statusBarHeight = systemInfo.statusBarHeight; //系统状态栏高度
		let navHeight = systemInfo.platform == 'ios' ? 44 : 48; //系统导航栏高度
		let sysNavHeight = statusBarHeight + navHeight; //导航栏+系统状态栏高度
		let deviceRatio = systemInfo.windowWidth / 750;
		that.globalData.statusBarHeight = statusBarHeight;
		that.globalData.navHeight = navHeight;
		that.globalData.sysNavHeight = sysNavHeight;
		that.globalData.sysNavHeight = sysNavHeight;
		that.globalData.deviceRatio = deviceRatio;
		this.setStorageSyncVal('navHeight', navHeight)
		this.setStorageSyncVal('statusBarHeight', statusBarHeight)
		this.setStorageSyncVal('sysNavHeight', sysNavHeight)
		this.setStorageSyncVal('deviceRatio', deviceRatio)
	},
	//获取缓存数据
	getStorageSyncVal: function(key) {
		let keyVal;
		try {
			keyVal = uni.getStorageSync(key);
		} catch (e) {}
		return keyVal;
	},
	//设置缓存数据
	setStorageSyncVal: function(key, val) {
		try {
			uni.setStorageSync(key, val);
			// console.log('设置缓存'+key+'成功')
		} catch (e) {}
	},
	//移除指定缓存
	removeStorageSyncVal: function(key) {
		try {
			uni.removeStorageSync(key);
		} catch (e) {
			// error
		}
	},
	//返回
	appBack: function(msg, delta = 1, time = 1300) {
		if (msg) {
			this.msg(msg);
		}
		setTimeout(function() {
			uni.navigateBack({
				delta: delta
			});
		}, time);
	},
	//跳转页面
	href: function(url, options = {}) {
		let type = options.type || 1; //跳转方式
		let ver = options.ver || false; //验证是否有该路由地址
		let params = options.params || false; //跳转携带参数
		let time = options.time || 0; //延迟跳转
		let msg = options.msg || ''; //提示文字
		let login = options.login || ''; //验证登录
		let token = uni.getStorageSync('token');
		if (!token && login) {
			this.goLogin();
			return false;
		}
		//type
		//1：navigateTo 保留当前页面，跳转到应用内的某个页面。(不可跳转到tabBar页面)
		//2：redirectTo 关闭当前页面，跳转到应用内的某个页面。(不可跳转到tabBar页面)
		//3：reLaunch   关闭所有页面，打开到应用内的某个页面。(可跳转到tabBar页面)
		//4：switchTab  跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
		if (ver) {
			if (!url) {
				this.msg('空地址')
				return false;
			}
		}
		//拼接参数
		if (params) {
			url = url + '?';
			for (let key in params) {
				url += key + '=' + params[key] + '&';
			}
			url = url.slice(0, url.length - 1);
		}
		//提示文字
		if (msg) {
			time = time || 3000;
			uni.showToast({
				title: msg,
				icon: 'none',
				mask: true,
				duration: time
			});
		}
		//跳转
		switch (type) {
			case 1:
				setTimeout(() => {
					uni.navigateTo({
						url: url
					})
				}, time)
				break;
			case 2:
				setTimeout(() => {
					uni.redirectTo({
						url: url
					})
				}, time)
				break;
			case 3:
				setTimeout(() => {
					uni.reLaunch({
						url: url
					})
				}, time)
				break;
			case 4:
				setTimeout(() => {
					uni.switchTab({
						url: url
					})
				}, time)
				break;
		}
	},
	//分页状态
	pageState: function(list, data, limit = 10) {
		//list：列表所有数据条数
		//limit：每页多少条
		//data：本次请求返回多少条数据
		let status;
		if (list == 0) {
			status = 'noData';
		} else if (list % limit > 0 || (list % limit == 0 && data == 0)) {
			status = 'noMore';
		} else {
			status = 'more';
		}
		return status;
	},
	//msg提示
	msg: function(title, duration = 1300, mask = false, icon = 'none') {
		//统一提示方便全局修改
		if (Boolean(title) === false) {
			return;
		}
		uni.showToast({
			title,
			duration,
			mask,
			icon
		});
	},
	//提示 请求中
	msgLoading: function(mask = true) {
		uni.showLoading({
			mask: mask
		});
	},
	//精度浮点乘法
	accMultiply: function(arg1, arg2) {
		let m = 0,
			s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			m += s1.split('.')[1].length;
		} catch (e) {
			// console.log(e)
		}
		try {
			m += s2.split('.')[1].length;
		} catch (e) {
			// console.log(e)
		}
		return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
	},
	/*加法函数，用来得到精确的加法结果*/
	accAdd: function(arg1, arg2) {
		let r1, r2, m, c;
		try {
			r1 = arg1.toString().split('.')[1].length;
		} catch (e) {
			r1 = 0;
		}
		try {
			r2 = arg2.toString().split('.')[1].length;
		} catch (e) {
			r2 = 0;
		}
		c = Math.abs(r1 - r2);
		m = Math.pow(10, Math.max(r1, r2));
		if (c > 0) {
			let cm = Math.pow(10, c);
			if (r1 > r2) {
				arg1 = Number(arg1.toString().replace('.', ''));
				arg2 = Number(arg2.toString().replace('.', '')) * cm;
			} else {
				arg1 = Number(arg1.toString().replace('.', '')) * cm;
				arg2 = Number(arg2.toString().replace('.', ''));
			}
		} else {
			arg1 = Number(arg1.toString().replace('.', ''));
			arg2 = Number(arg2.toString().replace('.', ''));
		}
		return (arg1 + arg2) / m;
	},
	/*减法函数，用来得到精确的减法结果*/
	accSub: function(arg1, arg2) {
		var r1, r2, m, n;
		try {
			r1 = arg1.toString().split('.')[1].length;
		} catch (e) {
			r1 = 0;
		}
		try {
			r2 = arg2.toString().split('.')[1].length;
		} catch (e) {
			r2 = 0;
		}
		m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
		n = (r1 >= r2) ? r1 : r2;
		return ((arg1 * m - arg2 * m) / m).toFixed(n);
	},
	//查找某属性的值在数组是否含有 并返回
	findArr(name, val, arr = []) {
		let obj = '';
		let ids = '';
		for (let i = 0; i < arr.length; i++) {
			if (arr[i][name] == val) {
				obj = arr[i];
				ids = i;
				break
			}
		}
		return {
			obj: obj,
			index: ids,
			arr: arr
		};
	},
	// 深度克隆 - uview拿来的
	deepClone(obj) {
		// 对常见的“非”值，直接返回原来值
		if ([null, undefined, NaN, false].includes(obj)) return obj;
		if (typeof obj !== "object" && typeof obj !== 'function') {
			//原始类型直接返回
			return obj;
		}
		var o = Object.prototype.toString.call(obj) === '[object Array]' ? [] : {};
		for (let i in obj) {
			if (obj.hasOwnProperty(i)) {
				o[i] = typeof obj[i] === "object" ? this.deepClone(obj[i]) : obj[i];
			}
		}
		return o;
	},
	//必填验证
	IsEmpty: function(str) {
		return (str == null || str.length < 1) ? false : true;
	},
	//验证手机号
	IsPhone: function(phone) {
		return /^1[3456789]\d{9}$/.test(phone);
	},
	//银行卡号（10到30位, 覆盖对公/私账户, 参考[微信支付]
	IsBank: function(str) {
		var reg = /^[1-9]\d{9,29}$/;
		return reg.test(str);
	},
	//验证正整数
	IsIntegerPositive: function(str) {
		var reg = /^[0-9]*[1-9][0-9]*$/;
		return reg.test(str);
	},

	//传时间戳 返回数组形式的日期
	getMyDate: function(str, type = '') {
		str = str ? str + '' : '';
		str = str ? str.length < 11 ? parseInt(str) * 1000 : parseInt(str) : '';
		var oDate = str ? new Date(str) : new Date(),
			Year = oDate.getFullYear(),
			Month = oDate.getMonth() + 1,
			Day = oDate.getDate(),
			Hour = oDate.getHours(),
			Min = oDate.getMinutes(),
			Sen = oDate.getSeconds(),
			week = oDate.getDay();
		if (type == 1) {
			return Year + '-' + addZero(Month) + '-' + addZero(Day) + ' ' + addZero(Hour) + ':' + addZero(Min) +
				':' + addZero(Sen);
		} else {
			return [Year, addZero(Month), addZero(Day), addZero(Hour), addZero(Min), addZero(Sen), week];
		}


		function addZero(num) {
			if (parseInt(num) < 10) {
				num = '0' + num;
			}
			return num;
		}
	},
};
